package com.twentyOne.ClothingLand.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.twentyOne.ClothingLand.dao.BaseDAO;
import com.twentyOne.ClothingLand.dao.IStorageDAO;
import com.twentyOne.ClothingLand.vo.Storage;

public class StorageDAO extends BaseDAO implements IStorageDAO {

	@Override
	public void addStorage(Storage storage) {
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.save(storage);
		tx.commit();
		session.close();
		
	}

	@Override
	public List<Storage> searchAllStorage() {

		Session session = getSession();
		String hql = "from Storage";
		Query query = session.createQuery(hql);
		List<Storage> list = query.list();
		session.close();
		if(list.size() != 0)
			return list;
		else 
			return null;
		
	}

	@Override
	public Storage searchStorageByID(int storageId) {

		Session session = getSession();
		String hql = "from Storage as s where s.storageId = " + storageId;
		Query query = session.createQuery(hql);
		List<Storage> list = query.list();
		session.close();
		if(list.size() != 0) {
			Storage storage = list.get(0);
			return storage;
		}else {
			return null;
		}
	
	}

}
